package com.chianghao.qyt.controller.manage_api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chianghao.frame.core.model.Page;
import com.chianghao.qyt.common.entity.BaseTenant;
import com.chianghao.qyt.common.entity.BaseTenantAccount;
import com.chianghao.qyt.controller.manage_api.dto.ResetTenantAccountPasswordDto;
import com.chianghao.qyt.controller.manage_api.dto.TenantAddDto;
import com.chianghao.qyt.controller.manage_api.dto.TenantModifyDto;
import com.chianghao.qyt.controller.manage_api.dto.TenantPageDto;
import com.chianghao.qyt.controller.manage_api.vo.TenantPageVo;
import com.chianghao.qyt.service.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("manage/api/tenant")
public class TenantController {

    @Resource
    TenantService tenantService;


    @Operation(summary = "添加")
    @PostMapping("add")
    public Boolean add(@RequestBody TenantAddDto dto){
        tenantService.add(dto.getName(),dto.getMobilePhone(),dto.getRemark());
        return true;
    }

    @Operation(summary = "修改")
    @PostMapping("modify")
    public Boolean modify(@RequestBody TenantModifyDto dto){
        tenantService.modify(dto.getId(),dto.getName(),dto.getMobilePhone(),dto.getRemark());
        return true;
    }


    @Operation(summary = "分页查询")
    @GetMapping("page")
    public Page<TenantPageVo> page(TenantPageDto dto){
        IPage<BaseTenant> page = this.tenantService.page(dto.getPageIndex(),dto.getPageSize(),dto.getCode(),dto.getName());
        Page<TenantPageVo> pageVo = new Page<>();
        if(page!=null){
            pageVo.setTotal(page.getTotal());
            if(page.getRecords()!=null){
                // 加载账号信息
                Set<Long> ids = page.getRecords().stream().map(BaseTenant::getId).collect(Collectors.toSet());
                Map<Long, BaseTenantAccount> tenantDefaultAccountMap = this.tenantService.queryTenantDefaultAccountMap(ids);

                // 组装返回的报文
                pageVo.setItems(page.getRecords().stream().map(i->{
                    TenantPageVo vo = new TenantPageVo();
                    vo.setId(i.getId());
                    vo.setName(i.getTenantName());
                    vo.setCode(i.getTenantCode());
                    vo.setState(i.getState());
                    vo.setCreateTime(i.getCreateTime());
                    vo.setRemark(i.getRemark());
                    vo.setMobilePhone(i.getMobilePhone());
                    BaseTenantAccount baseTenantAccount = tenantDefaultAccountMap.get(i.getId());
                    if(baseTenantAccount!=null){
                        vo.setAccount(baseTenantAccount.getAccount());
                        vo.setAccountId(baseTenantAccount.getId());
                    }
                    return vo;
                }).toList());
            }
        }
        return pageVo;
    }

    @Operation(summary = "设置商户的状态")
    @PostMapping("{tenantId}/state/setting")
    public Boolean setState(@PathVariable("tenantId") Long tenantId,@RequestParam("state") Boolean state){
        tenantService.setState(tenantId,state);
        return true;
    }


    @Operation(summary = "设置商户账号的密码")
    @PostMapping("account/password/reset")
    public Boolean setTenantAccountPassword(@RequestBody ResetTenantAccountPasswordDto dto){
        tenantService.resetAccountPassword(dto.getTenantAccountId(),dto.getPassword());
        return true;
    }


}
